﻿Imports System.Xml

Namespace DotNetNuke.Modules.ActiveForums
    Public Class ConfigUtils
        Public Function EnableRewriter(ByVal configPath As String) As Boolean
            Try
                Dim xDoc As New XmlDocument
                xDoc.Load(configPath)
                If Not xDoc Is Nothing Then
                    Dim xRoot As System.Xml.XmlNode = xDoc.DocumentElement
                    Dim xNode As System.Xml.XmlNode = xRoot.SelectSingleNode("//system.webServer/modules")
                    If Not xNode Is Nothing Then
                        If xNode.Attributes("runAllManagedModulesForAllRequests") Is Nothing Then
                            Dim xAttrib As XmlAttribute = xDoc.CreateAttribute("runAllManagedModulesForAllRequests")
                            xAttrib.Value = "true"
                            xNode.Attributes.Append(xAttrib)
                        End If
                        Dim isInstalled As Boolean = False
                        For Each n As XmlNode In xNode.ChildNodes
                            If n.Attributes("name").Value = "ForumsReWriter" Then
                                isInstalled = True
                                Exit For
                            End If
                        Next
                        If Not isInstalled Then
                            Dim xNewNode As XmlElement = xDoc.CreateElement("add")
                            Dim xAttrib As XmlAttribute = xDoc.CreateAttribute("name")
                            xAttrib.Value = "ForumsReWriter"
                            xNewNode.Attributes.Append(xAttrib)
                            xAttrib = xDoc.CreateAttribute("type")
                            xAttrib.Value = "DotNetNuke.Modules.ActiveForums.ForumsReWriter, DotNetNuke.Modules.ActiveForums"
                            xNewNode.Attributes.Append(xAttrib)
                            xAttrib = xDoc.CreateAttribute("preCondition")
                            xAttrib.Value = "managedHandler"
                            xNewNode.Attributes.Append(xAttrib)
                            xNode.PrependChild(xNewNode)
                            xDoc.Save(configPath)
                        End If
                    End If
                End If
                Return True
            Catch ex As Exception
                Return False
            End Try


        End Function
        Public Function DisableRewriter(ByVal configPath As String) As Boolean
            Try
                Dim xDoc As New XmlDocument
                xDoc.Load(configPath)
                If Not xDoc Is Nothing Then
                    Dim xRoot As System.Xml.XmlNode = xDoc.DocumentElement
                    Dim xNode As System.Xml.XmlNode = xRoot.SelectSingleNode("//system.webServer/modules")
                    If Not xNode Is Nothing Then
                        Dim isInstalled As Boolean = False
                        For Each n As XmlNode In xNode.ChildNodes
                            If n.Attributes("name").Value = "ForumsReWriter" Then
                                xNode.RemoveChild(n)
                                isInstalled = True
                                Exit For
                            End If
                        Next
                        If isInstalled Then
                            xDoc.Save(configPath)
                        End If
                    End If
                End If
                Return True
            Catch ex As Exception
                Return False
            End Try


        End Function
    End Class
End Namespace

